package com.sw.small_world.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhaoshuo on 2016/5/12.
 */
public class DateUtils {


    /**
     * String 转化 Date
     *
     * @param time
     * @return
     */
    public static Date stringConvertDate(String time, int temp) {
        if (time == null || "".equals(time)) {
            return null;
        }

        SimpleDateFormat sdf = null;

        if (temp == 0) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        } else if (temp == 1) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        } else if (temp == 2) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        } else if (temp == 3) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if (temp == 4) {
            sdf = new SimpleDateFormat("yyyy-MM");
        } else if (temp == 5) {
            sdf = new SimpleDateFormat("yyyy");
        } else {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }

        try {
            return sdf.parse(time);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * Date 转化 String
     *
     * @param time
     * @return
     */
    public static String dateConvertString(Date time, int temp) {
        if (time == null) {
            return null;
        }

        SimpleDateFormat sdf = null;

        if (temp == 0) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.format(time);

        } else if (temp == 1) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH");
            sdf.format(time);

        } else if (temp == 2) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            sdf.format(time);
        } else if (temp == 3) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.format(time);
        } else if (temp == 4) {
            sdf = new SimpleDateFormat("yyyy-MM");
            sdf.format(time);
        } else if (temp == 5) {
            sdf = new SimpleDateFormat("yyyy");
            sdf.format(time);
        } else if (temp == 6) {
            sdf = new SimpleDateFormat("MM");
            sdf.format(time);
        } else if (temp == 7) {
            sdf = new SimpleDateFormat("dd");
            sdf.format(time);
        } else if (temp == 8) {
            sdf = new SimpleDateFormat("HH");
            sdf.format(time);
        } else if (temp == 9) {
            sdf = new SimpleDateFormat("mm");
            sdf.format(time);
        } else if (temp == 10) {
            sdf = new SimpleDateFormat("HH:mm");
            sdf.format(time);
        } else if (temp == 11) {
            sdf = new SimpleDateFormat("MM月dd日");
            sdf.format(time);
        } else if (temp == 12) {
            sdf = new SimpleDateFormat("MM-dd HH:mm");
            sdf.format(time);
        } else {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.format(time);
        }

        try {
            return sdf.format(time);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 判断两个时间的大小，第一个参数大于等于的二个参数时，返回true
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean toJudge(Date date1, Date date2) {
        long _date1 = 0L;
        long _date2 = 0L;
        if (date1 != null) {
            _date1 = date1.getTime();
        }
        if (date2 != null) {
            _date2 = date2.getTime();
        }
        return (_date1 >= _date2);
    }

    /**
     * 根据data生成格式化时间串
     *
     * @param date
     * @return
     */
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }

    /**
     * 获取当前的时间
     *
     * @return
     */
    public static String getNowTime() {
        Date date = new Date(System.currentTimeMillis());
        return getTime(date);
    }

    public static final int YEAR_LENGTH = 1;
    public static final int MONTH_LENGTH = 2;
    public static final int DAY_LENGTH = 3;
    public static String changeDateType(Date date,int type){
        String dateString = DateUtils.getTime(date);
        String returnString = null;
        switch (type){
            case YEAR_LENGTH:
                returnString = dateString.substring(0,4);
                break;
            case MONTH_LENGTH:
                returnString = dateString.substring(0,7);
                break;
            case DAY_LENGTH:
                returnString = dateString.substring(0,10);
                break;
        }
        return returnString;
    }
}
